package configrations

import "time"

type SqliteConfigration struct {
	FileName        string
	MaxOpenConns    int           // 默认 1
	MaxIdleConns    int           // 默认 1
	ConnMaxLifetime time.Duration // 默认 0
	LogLevel        string        // 默认 "info"
}
type SqliteConfigrationOption func(*SqliteConfigration)

func NewSqliteConfigation(fileName string, opts ...SqliteConfigrationOption) *SqliteConfigration {
	// 设置默认值
	config := &SqliteConfigration{
		FileName:        fileName,
		MaxOpenConns:    1,
		MaxIdleConns:    1,
		ConnMaxLifetime: 0,
		LogLevel:        "info",
	}

	// 应用所有选项
	for _, opt := range opts {
		opt(config)
	}

	return config
}

// WithMaxOpenConns 设置最大打开连接数
func WithMaxOpenConns(n int) SqliteConfigrationOption {
	return func(c *SqliteConfigration) {
		c.MaxOpenConns = n
	}
}

// WithMaxIdleConns 设置最大空闲连接数
func WithMaxIdleConns(n int) SqliteConfigrationOption {
	return func(c *SqliteConfigration) {
		c.MaxIdleConns = n
	}
}

// WithConnMaxLifetime 设置连接最大存活时间
func WithConnMaxLifetime(d time.Duration) SqliteConfigrationOption {
	return func(c *SqliteConfigration) {
		c.ConnMaxLifetime = d
	}
}

// WithLogLevel 设置日志级别
func WithLogLevel(level string) SqliteConfigrationOption {
	return func(c *SqliteConfigration) {
		c.LogLevel = level
	}
}

// WithMemoryDB 快捷方式：创建内存数据库配置
func WithMemoryDB() SqliteConfigrationOption {
	return func(c *SqliteConfigration) {
		c.FileName = ":memory:"
	}
}
